/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.SdBountyHunter.Listeners;

import com.SySammy.SdBountyHunter.Handlers.BountyHandler;
import com.SySammy.SdBountyHunter.Prop.BountyHunterProperties;
import com.SySammy.SdBountyHunter.sdLaw;
import com.SySammy.SdCurrency.sdCCommandExport;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityListener;

public class EListener extends EntityListener {

    public sdLaw plugin;

    public EListener(sdLaw aThis) {
        this.plugin = aThis;
    }
    private final sdCCommandExport CE = new sdCCommandExport();

    @Override
    public void onEntityDamage(EntityDamageEvent event) {
        try {
            if (event instanceof EntityDamageByEntityEvent) {
                EntityDamageByEntityEvent sub = (EntityDamageByEntityEvent) event;
                if ((sub.getDamager() instanceof Player) && (sub.getEntity() instanceof Player)) {
                    BountyHandler BH = new BountyHandler(plugin);
                    Player attacker = (Player) sub.getDamager();
                    final Player badguy = (Player) sub.getEntity();
                    final String BadName = badguy.getName();
                    if (attacker.getItemInHand().getType() != Material.GOLD_SWORD) {
                        return;
                    }
                    if (BH.GetGoodGuy(BadName) == null) {
                        return;
                    }
                    String bountymakerName = BH.GetGoodGuy(BadName);
                    event.setDamage(0);
                    if (BadName.equals(bountymakerName)) {
                        return;
                    }

                    PrisonMaker PM = new PrisonMaker(plugin);
                    badguy.teleport(new Location(PM.PrisonLocation(bountymakerName).getWorld(), PM.PrisonLocation(bountymakerName).getX(), PM.PrisonLocation(bountymakerName).getY() + 1, PM.PrisonLocation(bountymakerName).getZ()));
                    attacker.sendMessage(ChatColor.DARK_AQUA + "Congrats, you caught a wanted man.");
                    attacker.sendMessage(ChatColor.DARK_AQUA + "Bounty won: " + ChatColor.GOLD + BH.GetBounty(BadName) + " " + CE.GetCurrency());
                    CE.GiveMoney(attacker.getName(), BH.GetBounty(BadName));
                    BH.SetCaught(badguy);

                    if (BadName.equalsIgnoreCase(bountymakerName)) {
                        PM.fillChest(bountymakerName, badguy);
                        CE.RemoveLoan(BadName);
                    } else {
                        PM.fillChest(bountymakerName, badguy);
                        Player bountymaker = plugin.getServer().getPlayer(bountymakerName);
                        if (bountymaker != null) {
                            bountymaker.sendMessage(ChatColor.DARK_AQUA + "You have a new prisoner, please check your prisons chest.");
                        }
                    }

                    badguy.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {

                        @Override
                        public void run() {
                            BountyHandler BH = new BountyHandler(plugin);
                            BH.RemoveBounty(BadName);
                            plugin.getServer().getPlayer(BadName).sendMessage(ChatColor.GREEN + "You are now free to go.");
                        }
                    }, 20L * 60 * BountyHunterProperties.arrestTime);
                }
            }
        } catch (Exception e) {
        }
    }
}
